const path = require('path')
const builder = require('electron-builder')
const Platform = builder.Platform
const builderConfig = require('../builder.config')

module.exports = (options) => {
    const targetType = options.target === 'dir' ? builder.DIR_TARGET : null
    const Arch = options.arch === 'x64' ? builder.Arch.x64 : builder.Arch.ia32
    
    let prepackagedPath
    if (options.target === 'prepackaged') {
        prepackagedPath = options.arch === 'x64' 
            ? path.join(__dirname, '../build/win-unpacked')
            : path.join(__dirname, '../build/win-ia32-unpacked')
    }

    const env = builderConfig.isMultiEnv ? options.env : 'prod'
    const appInfo = builderConfig[env]
    const config = { 
        ...appInfo,
        ...builderConfig.config,
        extraMetadata: {
            name: appInfo.productName,
            version: options.version
        }
    }

    config.win['signingHashAlgorithms'] = options.codeSigning ? ['sha256'] : []

    builder.build({
        targets: Platform.WINDOWS.createTarget(targetType, Arch),
        prepackaged: prepackagedPath,
        config
    }).catch(err => {
        console.error(err)
    })
}